package com.pybox.spec;

import com.pybox.entity.TMenu;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TMenu Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TMenuSpec implements Specification<TMenu> {

    private Map<String, String> params;

    public TMenuSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TMenu> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        String menuId = this.params.get("menuId");
        String menuName = this.params.get("menuName");
        String parentId = this.params.get("parentId");
        String orderNum = this.params.get("orderNum");
        String url = this.params.get("url");
        String menuType = this.params.get("menuType");
        String visible = this.params.get("visible");
        String perms = this.params.get("perms");
        String icon = this.params.get("icon");
        String createBy = this.params.get("createBy");
        String createTime = this.params.get("createTime");
        String updateBy = this.params.get("updateBy");
        String updateTime = this.params.get("updateTime");
        String remark = this.params.get("remark");
        List<Predicate> ps = new ArrayList<>();

        if (!StringUtils.isEmpty(menuId)) {
            ps.add(criteriaBuilder.like(root.get("menuId"), "%" + menuId + "%"));
        }
        if (!StringUtils.isEmpty(menuName)) {
            ps.add(criteriaBuilder.like(root.get("menuName"), "%" + menuName + "%"));
        }
        if (!StringUtils.isEmpty(parentId)) {
            ps.add(criteriaBuilder.like(root.get("parentId"), "%" + parentId + "%"));
        }
        if (!StringUtils.isEmpty(orderNum)) {
            ps.add(criteriaBuilder.like(root.get("orderNum"), "%" + orderNum + "%"));
        }
        if (!StringUtils.isEmpty(url)) {
            ps.add(criteriaBuilder.like(root.get("url"), "%" + url + "%"));
        }
        if (!StringUtils.isEmpty(menuType)) {
            ps.add(criteriaBuilder.like(root.get("menuType"), "%" + menuType + "%"));
        }
        if (!StringUtils.isEmpty(visible)) {
            ps.add(criteriaBuilder.like(root.get("visible"), "%" + visible + "%"));
        }
        if (!StringUtils.isEmpty(perms)) {
            ps.add(criteriaBuilder.like(root.get("perms"), "%" + perms + "%"));
        }
        if (!StringUtils.isEmpty(icon)) {
            ps.add(criteriaBuilder.like(root.get("icon"), "%" + icon + "%"));
        }
        if (!StringUtils.isEmpty(createBy)) {
            ps.add(criteriaBuilder.like(root.get("createBy"), "%" + createBy + "%"));
        }
        if (!StringUtils.isEmpty(createTime)) {
            ps.add(criteriaBuilder.like(root.get("createTime"), "%" + createTime + "%"));
        }
        if (!StringUtils.isEmpty(updateBy)) {
            ps.add(criteriaBuilder.like(root.get("updateBy"), "%" + updateBy + "%"));
        }
        if (!StringUtils.isEmpty(updateTime)) {
            ps.add(criteriaBuilder.like(root.get("updateTime"), "%" + updateTime + "%"));
        }
        if (!StringUtils.isEmpty(remark)) {
            ps.add(criteriaBuilder.like(root.get("remark"), "%" + remark + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}